package carapace
import (
"net/url"
"strings"
"github.com/carapace-sh/carapace/internal/common"
"github.com/carapace-sh/carapace/internal/export"
_shell "github.com/carapace-sh/carapace/internal/shell"
"github.com/carapace-sh/carapace/pkg/match"
)
type InvokedAction struct {
action Action
}
func (ia InvokedAction ) export () export .Export {
return export .Export {Meta : ia .action .meta , Values : ia .action .rawValues }
}
func (ia InvokedAction ) Filter (values ...string ) InvokedAction {
ia .action .rawValues = ia .action .rawValues .Filter (values ...)
return ia
}
func (ia InvokedAction ) Merge (others ...InvokedAction ) InvokedAction {
for _ , other := range append ([]InvokedAction {ia }, others ...) {
ia .action .rawValues = append (ia .action .rawValues , other .action .rawValues ...)
ia .action .meta .Merge (other .action .meta )
}
ia .action .rawValues = ia .action .rawValues .Unique ()
return ia
}
func (ia InvokedAction ) Prefix (prefix string ) InvokedAction {
for index , val := range ia .action .rawValues {
ia .action .rawValues [index ].Value = prefix + val .Value
}
return ia
}
func (ia InvokedAction ) Retain (values ...string ) InvokedAction {
ia .action .rawValues = ia .action .rawValues .Retain (values ...)
return ia
}
func (ia InvokedAction ) Suffix (suffix string ) InvokedAction {
for index , val := range ia .action .rawValues {
ia .action .rawValues [index ].Value = val .Value + suffix
}
return ia
}
func (ia InvokedAction ) UidF (f func (s string ) (*url .URL , error )) InvokedAction {
for index , v := range ia .action .rawValues {
url , err := f (v .Value )
if err != nil {
return ActionMessage (err .Error()).Invoke (Context {})
}
ia .action .rawValues [index ].Uid = url .String ()
}
return ia
}
func (ia InvokedAction ) ToA () Action {
return ia .action
}
func tokenize(s string , dividers ...string ) []string {
if len (dividers ) == 0 {
return []string {s }
}
result := make ([]string , 0 )
for _ , word := range strings .SplitAfter (s , dividers [0 ]) {
tokens := tokenize (strings .TrimSuffix (word , dividers [0 ]), dividers [1 :]...)
if len (tokens ) > 0 && strings .HasSuffix (word , dividers [0 ]) {
tokens [len (tokens )-1 ] = tokens [len (tokens )-1 ] + dividers [0 ]
}
result = append (result , tokens ...)
}
return result
}
func (ia InvokedAction ) ToMultiPartsA (dividers ...string ) Action {
return ActionCallback (func (c Context ) Action {
splittedCV := tokenize (c .Value , dividers ...)
uniqueVals := make (map [string ]common .RawValue )
for _ , val := range ia .action .rawValues {
if match .HasPrefix (val .Value , c .Value ) {
if splitted := tokenize (val .Value , dividers ...); len (splitted ) >= len (splittedCV ) {
v := strings .Join (splitted [:len (splittedCV )], "" )
d := splitted [len (splittedCV )-1 ]
if len (splitted ) == len (splittedCV ) {
uniqueVals [v ] = common .RawValue {
Value : v ,
Display : d ,
Description : val .Description ,
Style : val .Style ,
Tag : val .Tag ,
Uid : val .Uid ,
}
} else {
uniqueVals [v ] = common .RawValue {
Value : v ,
Display : d ,
Description : "" ,
Style : "" ,
Tag : val .Tag ,
Uid : val .Uid ,
}
}
}
}
}
vals := make ([]common .RawValue , 0 )
for _ , val := range uniqueVals {
vals = append (vals , val )
}
a := Action {rawValues : vals }
for _ , divider := range dividers {
if runes := []rune (divider ); len (runes ) == 0 {
a .meta .Nospace .Add ('*' )
break
} else {
a .meta .Nospace .Add (runes [len (runes )-1 ])
}
}
return a
})
}
func (ia InvokedAction ) value (shell string , value string ) string {
return _shell .Value (shell , value , ia .action .meta , ia .action .rawValues )
}
func init() {
common .FromInvokedAction = func (i interface {}) (common .Meta , common .RawValues ) {
if invoked , ok := i .(InvokedAction ); ok {
return invoked .action .meta , invoked .action .rawValues
}
return common .Meta {}, nil
}
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .